package com.igbeok.miscellaneous.cglib.person.proxy;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public abstract class CglibProxy implements MethodInterceptor {

	public <T> T getProxy(Class<T> proxyClass) {
		Enhancer enhancer = new Enhancer();
		enhancer.setSuperclass(proxyClass);
		enhancer.setCallback(this);

		return (T) enhancer.create();
	}

	@Override
	public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
		before();
		Object object = proxy.invokeSuper(obj, args);
		after();

		return object;
	}

	public abstract void before();

	public abstract void after();

}
